package org.wuzhou.lvyou.activity;

import org.wuzhou.lvyou.R;
import org.wuzhou.lvyou.base.BaseActivity;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;

public class DirectionActivity extends BaseActivity {
	private ImageView imageView;
	private SensorManager manager;
	private SensorListener listener=new SensorListener();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.direction);
		imageView = (ImageView) findViewById(R.id.im_Direction);
		imageView.setKeepScreenOn(true);
		manager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
		if(manager.getDefaultSensor(Sensor.TYPE_ORIENTATION)==null){
			Dialog dialog=new AlertDialog.Builder(DirectionActivity.this)
			.setTitle("返回主界面？")
			.setMessage("检测到手机并未提供相应的硬件支持，无法使用“指南针”功能，您确定要返回主界面吗？")
			.setPositiveButton("确定", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					DirectionActivity.this.gotoActivity(DirectionActivity.this, MainActivity.class);
				}
			}).create();//创建一个对话框
			dialog.show();//显示对话框
		}
	}

	@Override
	protected void onResume() {
		Sensor sensor = manager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
		
		manager.registerListener(listener, sensor,
				SensorManager.SENSOR_DELAY_GAME);
		super.onResume();
	}

	@Override
	protected void onPause() {
		manager.unregisterListener(listener);
		super.onPause();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if(keyCode==KeyEvent.KEYCODE_BACK){//返回键点击是弹出是否返回对话框
			_backToMain();
		}
		return false;
	}

	private void _backToMain(){//返回主界面函数
		Dialog dialog=new AlertDialog.Builder(DirectionActivity.this)
		.setTitle("返回主界面？")
		.setMessage("您确定要返回主界面吗？")
		.setPositiveButton("确定", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				DirectionActivity.this.gotoActivity(DirectionActivity.this, MainActivity.class);
			}
		}).setNegativeButton("取消",new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				
			}
		}).create();//创建一个对话框
		dialog.show();//显示对话框
	}
	
	private final class SensorListener implements SensorEventListener {//实现传感器监听器
		private float predegree = 0;

		@Override
		public void onAccuracyChanged(Sensor sensor, int accuracy) {

		}

		@Override
		public void onSensorChanged(SensorEvent event) {
			float degree = event.values[0];// 存方向值
			RotateAnimation animation = new RotateAnimation(predegree, -degree,
					Animation.RELATIVE_TO_SELF,0.5f,
					Animation.RELATIVE_TO_SELF,0.5f);
			animation.setDuration(200);
			imageView.startAnimation(animation);
			predegree = -degree;//反方向旋转

		}

	}
	
	public void goback(View v){
		gotoActivity(this, MainActivity.class);
	}
}
